package com.poker.duiued;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.poker.iuious.yhyhjs.jklimj.User;
import com.poker.jkiuus.R;
import com.poker.jkiuus.uyrfgd.AppManager;
import com.poker.jkiuus.uyrfgd.DatabaseHelper;
import com.poker.jkiuus.uyrfgd.MD5Utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LoginActivity extends AppCompatActivity {
    private EditText et_user_name;
    private EditText et_psw;
    private Button btn_login;
    private TextView tv_register;
    private TextView tv_find_psw;
    private TextView privacy;
    private String userName;
    private DatabaseHelper databaseHelper;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        AppManager.addActivity(this);
        databaseHelper = new DatabaseHelper(this);
        initView();


    }

    public void initView(){
        et_user_name = findViewById(R.id.et_user_name);
        et_psw = findViewById(R.id.et_psw);
        btn_login = findViewById(R.id.btn_login);
        tv_register = findViewById(R.id.tv_register);
        tv_find_psw = findViewById(R.id.tv_find_psw);
        privacy = findViewById(R.id.privacy);

        btn_login.setOnClickListener(v -> {
            String nameOrEmail = et_user_name.getText().toString().trim();
            String password = et_psw.getText().toString().trim();
            password = MD5Utils.string2MD5(password);
            boolean login = databaseHelper.login(new User(nameOrEmail, password, nameOrEmail));

            if (nameOrEmail.length() <= 0 || password.length() <= 0 ){
                showLoginDialog("The account number or password cannot be empty!",false);
            }else if (!login){
                showLoginDialog("The account number or password is incorrect!",false);
                et_psw.setError("The account number or password is incorrect!");
            }else {
                userName = nameOrEmail;
                String currentTime = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
                User user = databaseHelper.queryUserByNameOrEmail(userName);
                databaseHelper.updateLoginTime(user.getId(), currentTime);
                showLoginDialog("Login successful!",true);
            }
        });

        tv_register.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.slide_up, R.anim.slide_down);
            finish();
        });

        tv_find_psw.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this,FindPwsActivity.class);
            startActivity(intent);
        });
    }

    private void showLoginDialog(String message,boolean flag) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("PROMPT");
        builder.setMessage(message);
        builder.setPositiveButton("OK", (dialog, which) -> {
            if (flag) {
                Intent intent = new Intent(LoginActivity.this,AiActivity.class);
                intent.putExtra("USER_NAME",userName);
                startActivity(intent);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                dialog.dismiss();
                finish();
                super.finish();
            }
            dialog.dismiss();
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

}
